package com.weidong.mybbs.config;

import com.weidong.mybbs.interceptor.PermissionInterceptor;
import com.weidong.mybbs.interceptor.RequireLoginInterceptor;
import com.weidong.mybbs.interceptor.TmpSessionInterceptor;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Configuration;
import org.springframework.util.ResourceUtils;
import org.springframework.web.servlet.config.annotation.CorsRegistry;
import org.springframework.web.servlet.config.annotation.InterceptorRegistry;
import org.springframework.web.servlet.config.annotation.ResourceHandlerRegistry;
import org.springframework.web.servlet.config.annotation.WebMvcConfigurationSupport;

@Configuration
public class WebMvcConfig extends WebMvcConfigurationSupport {

    @Autowired
    private TmpSessionInterceptor tmpSessionInterceptor;
    @Autowired
    private RequireLoginInterceptor requireLoginInterceptor;
    @Autowired
    private PermissionInterceptor permissionInterceptor;

    /**
     * 设置跨域请求
     * @param registry
     */
    @Override
    protected void addCorsMappings(CorsRegistry registry) {
        super.addCorsMappings(registry);
        //设置允许跨域的路径
        registry.addMapping("/**")
                //设置允许跨域请求的域名
                .allowedOrigins("*")
                //是否允许证书 不再默认开启
                .allowCredentials(true)
                //设置允许的方法
                .allowedMethods("GET", "POST", "PUT", "DELETE")
                //跨域允许时间
                .maxAge(3600);
    }

    /**
     * 设置开放静态资源
     */
    @Override
    protected void addResourceHandlers(ResourceHandlerRegistry registry) {
        //配置静态资源处理
        registry.addResourceHandler("/static/**")
                .addResourceLocations(ResourceUtils.CLASSPATH_URL_PREFIX+"/META-INF/resources/")
                .addResourceLocations(ResourceUtils.CLASSPATH_URL_PREFIX+"/resources/")
                .addResourceLocations(ResourceUtils.CLASSPATH_URL_PREFIX+"/static/")
                .addResourceLocations(ResourceUtils.CLASSPATH_URL_PREFIX+"/public/");
        super.addResourceHandlers(registry);
    }


    @Override
    protected void addInterceptors(InterceptorRegistry registry) {
        //临时session
        registry.addInterceptor(tmpSessionInterceptor)
                .addPathPatterns("/**")
                .excludePathPatterns("/static/**")
                .excludePathPatterns("/error/**")
                .excludePathPatterns("/auth/logout/**");

        String[] excludePathPatterns = new String[]{
                "/auth/login/**",
                "/auth/doLogin/**",
                "/public/**",
                "/static/**",
                "/error/**"
        };
        //必须登录
        registry.addInterceptor(requireLoginInterceptor)
                .addPathPatterns("/**")
                .excludePathPatterns(excludePathPatterns);
        //权限
        registry.addInterceptor(permissionInterceptor)
                .addPathPatterns("/**")
                .excludePathPatterns(excludePathPatterns);
    }
}
